package com.gitee.feizns.quickstart.ding.talk.api.top.api.user;

import com.dingtalk.api.DefaultDingTalkClient;
import com.dingtalk.api.DingTalkClient;
import com.dingtalk.api.request.OapiUserListsimpleRequest;
import com.dingtalk.api.response.OapiUserListsimpleResponse;
import com.gitee.feizns.quickstart.ding.talk.utils.Utils;
import lombok.SneakyThrows;

import java.util.ArrayList;

/**
 * 获取部门用户基础信息。（/user/listsimple）
 * 接口路径：<a href="https://open.dingtalk.com/document/orgapp/queries-the-simple-information-of-a-department-user">获取部门用户基础信息（/topapi/v2/department/listsub）</a>
 * @link <a href="https://open.dingtalk.com/document/orgapp/queries-the-simple-information-of-a-department-user">服务端API 通讯录管理 用户管理 获取部门用户基础信息</a>
 * @author feizns
 * @since 2024/6/29
 */
public class ListSimple {

    /**
     * 默认大小
     */
    public static final long DEFAULT_SIZE = 100;

    /**
     * 调用本接口获取指定部门中的用户详细信息。
     * @param accessToken 访问令牌
     * @param req 要求
     * @return {@link OapiUserListsimpleResponse.PageResult}
     */
    @SneakyThrows
    public static OapiUserListsimpleResponse.PageResult execute(String accessToken, OapiUserListsimpleRequest req) {
        DingTalkClient client = new DefaultDingTalkClient("https://oapi.dingtalk.com/topapi/user/listsimple");
        OapiUserListsimpleResponse rsp = client.execute(req, accessToken);
        return Utils.ifErrorThrow(rsp).getResult();
    }

    /**
     * v2 用户列表
     * @param accessToken 访问令牌
     * @param deptId 部门编号
     * @return {@link java.util.List}<{@link OapiUserListsimpleResponse.ListUserSimpleResponse}>
     */
    @SneakyThrows
    public static java.util.List<OapiUserListsimpleResponse.ListUserSimpleResponse> execute(String accessToken, Long deptId) {
        java.util.List<OapiUserListsimpleResponse.ListUserSimpleResponse> result = new ArrayList<>();
        long cursor = 0;
        OapiUserListsimpleResponse.PageResult pageResult;
        do {
            pageResult = execute(accessToken, request(deptId, cursor, DEFAULT_SIZE));
            result.addAll(pageResult.getList());
            cursor++;
        } while ( pageResult.getHasMore() );
        return result;
    }

    /**
     * 请求
     * @param deptId 部门编号
     * @param cursor 光标
     * @param size 大小
     * @return {@link OapiUserListsimpleRequest}
     */
    public static OapiUserListsimpleRequest request(Long deptId, Long cursor, Long size) {
        return request(deptId, cursor, size, "modify_desc", false, "zh_CN");
    }

    /**
     * 请求
     * @param deptId 部门编号
     * @param cursor 光标
     * @param size 大小
     * @param modifyDesc 修改 DESC
     * @param containAccessLimit 包含访问限制
     * @param language 语言
     * @return {@link OapiUserListsimpleRequest}
     */
    public static OapiUserListsimpleRequest request(Long deptId, Long cursor, Long size, String modifyDesc, boolean containAccessLimit, String language) {
        OapiUserListsimpleRequest req = new OapiUserListsimpleRequest();
        req.setDeptId(deptId);
        req.setCursor(cursor);
        req.setSize(size);
        req.setOrderField(modifyDesc);
        req.setContainAccessLimit(containAccessLimit);
        req.setLanguage(language);
        return req;
    }

}
